<?php
    session_start();

    //error_reporting(0);// turn off errors on release
    $settings = parse_ini_file('troll.ini',true);
    $_SESSION['msg'] = '';

    require 'includes/dbconnect.php';
    require 'includes/functions.php';
    

    /* ***********************************************************************
     *      LOGIN 
     * ***********************************************************************/
    if(isset($_POST['login']))
    {
        $username       = strtoupper($_POST['username']);
        $password       = strtoupper($_POST['password']);
        
        /* MANGOS/WOW STYLE PW */
        $password       = md5($username.':'.$password);      

        if(correctLogin($username, $password) != null)
        {
            $qRecord = getUserRecord($username);
            // Check to see if they are banned
            if($qRecord['banned'] > 0)// >0 future ref for ban reasons
            {
                $_SESSION['msg'] = 
                    "<p class='error_msg'>This account has been banned.</p>";
            }
            else
            {
                /* LOGIN SUCCESSFUL - SET SESSION VARS */
                $_SESSION['loggedin']   = true;
                $_SESSION['curr_location'] = 'index';
                $_SESSION['afk']        = $settings['systemsettings']['afk'] + time();
                $_SESSION['id']         = $qRecord['id'];// global user id                
                header('location:playerselect.php');
                die();
            }
        }
        else
        {
            /* username & pw do not match */
            $_SESSION['msg'] = '<p class="error_msg">Invalid Login</p>';

        }
    }
    /* ***********************************************************************
     *      SIGNUP
     * ***********************************************************************/
    elseif(isset($_GET['signup']))
    {
        include('signup.php');
        die();
    }
   
?>

<html>
<head>
    <link rel="stylesheet" type="text/css" href="scripts/troll.css"/>
    <title>TROLL: Login</title>
</head>
<body>
    <center>
    <div class="console">        
        <table width="800" clas="console">
            <tr>
                <td>                
                    <center>
                    <img src='images/troll.bmp'/><br/>
                    <h1 class="title">TROLL</h1>
                    <?php if(isset($_SESSION['msg'])) echo $_SESSION['msg'];?>
                    <form method="post" action="index.php">
                        <input class="login" 
                                type="text" 
                                name="username" 
                                placeholder="UserName" autofocus/>

                        <input class="login" 
                                type="password" 
                                name="password" placeholder="Password"/>
                        <br>
                        <p><input type="submit" name="login" value="Login"/></p>
                        <a href="?signup">Sign Up To Play</a>
                    </form>
                    
                    </center>
                </td>
            </tr>
        </table>    
    </div>  
    </center>   
</body> 
